package univer.oko.database.rmi.impl;

import univer.oko.Error;
import java.util.Vector;

public class DirectionImpl extends ImplHelper implements DBMetadata {
	public DirectionImpl(RMIImpl rmi) {
        super(rmi);
    }
	
	public synchronized int addDirection(String name, String faculty) {
        if (this.isOccurs(SELECT + DIRECTION_ID + FROM + DIRECTION + WHERE + DIRECTION_NAME + " = '" + name + "'")) {
            return univer.oko.Error.REPEAT;
        }

        int count = this.getIntValue(SELECT + MAX(DIRECTION_ID) + FROM + DIRECTION) + 1;
        Integer f = this.getIntValue(SELECT + FACULTY_ID + FROM + FACULTY + WHERE + FACULTY_NAME + " = '" + faculty + "'");
        if(f == null)
          return Error.ALRIGHT;
        this.executeUpdate(INSERT_INTO + DIRECTION + " values(" + count + "," + f + ",'" + name + "')");
        this.commit();
        return Error.ALRIGHT;
    }

    public synchronized Vector getDirections() {
        return this.getVectorValue(SELECT + DIRECTION_NAME + FROM + DIRECTION + ORDER_BY + DIRECTION_NAME);
    }

    public synchronized Vector getDirections(String faculty) {
        return this.getVectorValue(
                SELECT + DIRECTION_NAME +
                        FROM + DIRECTION + ", " + FACULTY +
                        WHERE + DIRECTION_REF_FACULTY + " = " + FACULTY_ID + AND + FACULTY_NAME + " = '" + faculty + "' " +
                        ORDER_BY + DIRECTION_NAME);
    }

    public synchronized int deleteDirection(String str) {
        if (this.isOccurs(
                SELECT + " [student].[id_student] " +
                        FROM + "[student], " + DIRECTION +
                        WHERE + DIRECTION_NAME + " = '" + str + "' " + AND + DIRECTION_ID + " = [student].[ref_direction]"))
        {
            return Error.IN_USE;
        }

        if (this.executeUpdate(DELETE_FROM + DIRECTION + WHERE + DIRECTION_NAME + " = '" + str + "'")) {
            this.commit();
            return Error.ALRIGHT;
        }

        return Error.SQL_ERROR;
    }

    public synchronized Integer getDirectionID(String str) {
        return this.getIntValue(SELECT + DIRECTION_ID + FROM + DIRECTION + WHERE + DIRECTION_NAME + " = '" + str + "'");
    }

    public synchronized String getDirectionName(int id) {
        return this.getStringValue(SELECT + DIRECTION_NAME + FROM + DIRECTION + WHERE + DIRECTION_ID + " = " + id);
    }
}
